package cn.dnf.product.controller;

import cn.dnf.basic.exception.BusinessException;
import cn.dnf.basic.util.JsonResult;
import cn.dnf.basic.util.LoginContext;
import cn.dnf.basic.util.PageList;
import cn.dnf.product.domain.Product;
import cn.dnf.product.query.ProductQuery;
import cn.dnf.product.service.IProductService;
import cn.dnf.user.domain.Logininfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

// @Controller
// @ResponseBody
@RestController // controller层，返回json格式数据
@RequestMapping("/product")
@Api(value = "服务的API",description="服务相关的CRUD功能")
public class ProductController {

    @Autowired
    private IProductService ProductService;


    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询" )
    public Product findOne(@PathVariable("id") Long id){
        return ProductService.loadById(id);
    }

    /**
     * 删除操作
     * @param id
     * @return
     *   JsonResult ：链式编程
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除" )
    public JsonResult deleteById(@PathVariable("id") Long id){

        try {
            ProductService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟，稍后重试");
        }
    }

    /**
     * http://localhost:8080/Product
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或者修改" )
    public JsonResult updateOrAdd(@RequestBody Product product){

        try {
            if(product.getId()==null){
                // 添加操作
                ProductService.save(product);
            }else{
                // 修改操作
                ProductService.update(product);
            }
            return JsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     *  分页数据
     * @return
     */
    @ApiOperation(value = "分页查询接口" )
    @PostMapping
    public PageList<Product> queryPage(@RequestBody ProductQuery query){
        try {

            return ProductService.pageQuery(query);

        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<Product>();
        }
    }


    @PostMapping("/onsale")
    @ApiOperation(value = "上架" )
    public JsonResult onSale(@RequestBody List<Long> ids){

        try {
            ProductService.onSale(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟，稍后重试");
        }
    }

    @PostMapping("/offsale")
    @ApiOperation(value = "下架" )
    public JsonResult offSale(@RequestBody List<Long> ids){

        try {
            ProductService.offSale(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟，稍后重试");
        }
    }

}
